package com.nageoffer.shortlink.admin.controller;

import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.common.convention.result.Results;
import com.nageoffer.shortlink.admin.pojo.dto.req.UserLoginReqDTO;
import com.nageoffer.shortlink.admin.pojo.dto.req.UserRegisterReqDTO;
import com.nageoffer.shortlink.admin.pojo.dto.req.UserUpdateReqDTO;
import com.nageoffer.shortlink.admin.pojo.dto.resp.UserLoginRespDTO;
import com.nageoffer.shortlink.admin.pojo.dto.resp.UserRespDTO;
import com.nageoffer.shortlink.admin.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/short-link/admin/v1/user")
public class UserController {
    private final UserService userService;

    /**
     * 根据用户名查询用户信息（已脱敏）
     * @param username
     * @return
     */
    @GetMapping("/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable String username){
        UserRespDTO res = userService.getUserByUsername(username);
        return Results.success(res);
    }
    /**
     * 根据用户名查询无脱敏信息
     */

    /**
     * 查询用户名是否存在功能
     */
    @GetMapping("/has-username")
    public Result hasUsername(@RequestParam("username") String username){

        return Results.success( userService.availableUername(username));
    }

    /**
     * 新增用户
     */
    @PostMapping("/register")
    public Result userRegister(@RequestBody UserRegisterReqDTO requestParam){
        userService.userRegister(requestParam);
        return Results.success();
    }

    /**
     * 修改用户信息
     * @param requestParam
     * @return
     */
    @PutMapping
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam){
        userService.update(requestParam);
        return Results.success();
    }

    /**
     * 用户登录
     * @param requestParam
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam){
        UserLoginRespDTO resp = userService.login(requestParam);
        return Results.success(resp);
    }

    @PostMapping("/check-login")
    public Result<Boolean> checkLogin(
            @RequestParam("username")String username,
            @RequestParam("token")String token
    ){
        return Results.success(userService.checkLogin(username,token));
    }

    @DeleteMapping("/logout")
    public Result<Void> logout(@RequestParam("username") String username,@RequestParam("token")String token){
        userService.logout(username,token);
        return Results.success();
    }
}
